package com.stylefeng.guns.modular.manager.controller;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.manager.service.IGoodsCategoryService;
import com.stylefeng.guns.modular.manager.service.IManagerGoodsService;
import com.stylefeng.guns.modular.manager.service.IManagerSupplierService;
import com.stylefeng.guns.modular.stockmanagement.service.IGoodsDamageListService;
import com.stylefeng.guns.modular.stockmanagement.service.IGoodsDamageService;
import com.stylefeng.guns.modular.stockmanagement.service.IGoodsOverflowListService;
import com.stylefeng.guns.modular.stockmanagement.service.IGoodsOverflowService;
import com.stylefeng.guns.modular.system.model.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Objects;

/**
 * 商品录入管理控制器
 *
 * @author LiuXin
 * @Date 2018-09-12 17:32:27
 */
@Controller
@RequestMapping("/managerGoods")
public class ManagerGoodsController extends BaseController {

    private String PREFIX = "/manager/managerGoods/";

    @Autowired
    private IManagerGoodsService managerGoodsService;
    @Autowired
    private IManagerSupplierService managerSupplierService;
    @Autowired
    private IGoodsCategoryService categoryService;
    @Autowired
    private IGoodsDamageService goodsDamageService;
    @Autowired
    private IGoodsOverflowService overflowService;
    @Autowired
    private IGoodsDamageListService goodsDamageListService;
    @Autowired
    private IGoodsOverflowListService goodsOverflowListService;


    private static final Integer DAMAGE_TYPE = 1;
    private static final Integer OVERFLOW_TYPE = 2;


    /**
     * 跳转到商品录入管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "managerGoods.html";
    }

    /**
     * 跳转到添加商品录入管理
     */
    @RequestMapping("/managerGoods_add")
    public String managerGoodsAdd() {
        return PREFIX + "managerGoods_add.html";
    }

    /**
     * 跳转进库存预警页面
     */
    @RequestMapping("/goodsAlarm/index")
    public String goodsAlarmIndex() {
        return "/stockmanagement/goodsAlarm/goodsAlarmIndex.html";
    }

    /**
     * 跳转进报损报溢查询页面
     */
    @RequestMapping("/goodsDamageAndOverflow/index")
    public String goodsDamageAndOverflowIndex(){
        return "/stockmanagement/damageAndOverflowQuery/damageAndOverflowQueryIndex.html";
    }

    /**
     * 跳转进 报损报溢查询
     * @param orderId id
     * @param queryType 查询类型
     * @param orderNumber 订单号
     * @param model model
     * @return 页面
     */
    @RequestMapping("/goodsDamageAndOverflowOrder/index/{orderId}/{queryType}/{orderNumber}")
    public String goodsDamageAndOverflowOrderDetail(@PathVariable(value = "orderId")Integer orderId,
                                                   @PathVariable(value = "queryType")Integer queryType,
                                                   @PathVariable(value = "orderNumber")String orderNumber,
                                                   Model model){
        model.addAttribute("orderId", orderId);
        model.addAttribute("queryType", queryType);
        model.addAttribute("orderNumber", orderNumber);
        return "/stockmanagement/damageAndOverflowQuery/damageAndOverflowOrderIndex.html";
    }

    /**
     * 报损报溢查询
     * @param orderId id
     * @param queryType 查询类型
     * @return 结果集
     */
    @RequestMapping("/goodsDamageAndOverflowOrder/detail/{orderId}/{queryType}")
    @ResponseBody
    public Object goodsDamageAndOverflowOrderDetail(@PathVariable(value = "orderId")String orderId,
                                                    @PathVariable(value = "queryType")Integer queryType){
        //报损
        if (queryType.equals(DAMAGE_TYPE)) {
            EntityWrapper<GoodsDamageList> entityWrapper = new EntityWrapper<>();
            entityWrapper.eq("damage_id", orderId);
            Page<GoodsDamageList> page = new PageFactory<GoodsDamageList>().defaultPage();
            Page<GoodsDamageList> pageResult = goodsDamageListService.selectPage(page, entityWrapper);
            return this.packForBT(pageResult);
        //报溢
        } else if (queryType.equals(OVERFLOW_TYPE)) {
            EntityWrapper<GoodsOverflowList> entityWrapper = new EntityWrapper<>();
            entityWrapper.eq("goods_overflow_id", orderId);
            Page<GoodsOverflowList> page = new PageFactory<GoodsOverflowList>().defaultPage();
            Page<GoodsOverflowList> pageResult = goodsOverflowListService.selectPage(page, entityWrapper);
            return this.packForBT(pageResult);
        }
        return new PageInfoBT<>(new Page<>());
    }

    /**
     * 跳转到修改商品录入管理
     */
    @RequestMapping("/managerGoods_update/{managerGoodsId}")
    public String managerGoodsUpdate(@PathVariable Integer managerGoodsId, Model model) {
        ManagerGoods managerGoods = managerGoodsService.selectById(managerGoodsId);
        ManagerSupplier managerSupplier = managerSupplierService.selectById(managerGoods.getSupplierId());
        GoodsCategory goodsCategory = categoryService.selectById(managerGoods.getGoodsCategoryId());
        model.addAttribute("category", goodsCategory);
        model.addAttribute("supplier", managerSupplier);
        model.addAttribute("item", managerGoods);
        LogObjectHolder.me().set(managerGoods);
        return PREFIX + "managerGoods_edit.html";
    }

    /**
     * 获取商品录入管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(ManagerGoods managerGoods) {
        Page<ManagerGoods> page = new PageFactory<ManagerGoods>().defaultPage();
        EntityWrapper<ManagerGoods> entityWrapper = new EntityWrapper<>(managerGoods);
        if (null != managerGoods.getGoodsCategoryId()) {
            entityWrapper.eq("goods_category_id", managerGoods.getGoodsCategoryId());
        }
        //名字或者编码查询都放在这个字段里面
        if (StringUtils.isNotBlank(managerGoods.getGoodsCode())) {
            entityWrapper.
                    like("goods_code", managerGoods.getGoodsCode()).
                    or().
                    like("goods_name", managerGoods.getGoodsCode());
        }
        //供应商id
        if(null != managerGoods.getSupplierId()){
            entityWrapper.eq("supplier_id", managerGoods.getSupplierId());
        }
        Page<ManagerGoods> resultPage = managerGoodsService.selectWithPage(page, entityWrapper);
        PageInfoBT<ManagerGoods> pageInfoBT = this.packForBT(resultPage);
        return pageInfoBT;
    }

    @RequestMapping(value = "/goodsAlarm/list")
    @ResponseBody
    public Object goodsAlarmList(ManagerGoods managerGoods) {
        Page<ManagerGoods> page = new PageFactory<ManagerGoods>().defaultPage();
        managerGoods.setAlarmState(1);
        EntityWrapper<ManagerGoods> entityWrapper = new EntityWrapper<>(managerGoods);
        entityWrapper.like("goods_code", managerGoods.getGoodsCode());
        entityWrapper.like("goods_name", managerGoods.getGoodsName());
        //预警状态
        entityWrapper.eq("alarm_state", 1);
        Page<ManagerGoods> resultPage = managerGoodsService.selectAlarmPage(page, entityWrapper);
        PageInfoBT<ManagerGoods> pageInfoBT = this.packForBT(resultPage);
        return pageInfoBT;
    }

    /**
     * 跳转进当前库存查询
     * @return 页面
     */
    @RequestMapping(value = "/goodsStock/index")
    public String goodsStockIndex(){
        return "/stockmanagement/goodsStock/goodsStock.html";
    }

    @RequestMapping(value = "/goodsDamageAndOverflow/list")
    @ResponseBody
    public Object goodsDamageAndOverflowList(@RequestParam(value = "queryType", required = false) Integer queryType,
                                             @RequestParam(value = "orderNumber", required = false) String orderNumber) {
        //报损
        if (queryType.equals(DAMAGE_TYPE)) {
            GoodsDamage goodsDamage = new GoodsDamage();
            goodsDamage.setDamageOrderNumber(orderNumber);
            EntityWrapper<GoodsDamage> entityWrapper = new EntityWrapper<>(goodsDamage);
            entityWrapper.like("damage_order_number", orderNumber);
            Page<GoodsDamage> page = new PageFactory<GoodsDamage>().defaultPage();
            Page<GoodsDamage> pageResult = goodsDamageService.selectPageWithDataScope(page, entityWrapper);
            return this.packForBT(pageResult);
            //报溢
        } else if (queryType.equals(OVERFLOW_TYPE)) {
            GoodsOverflow goodsOverflow = new GoodsOverflow();
            goodsOverflow.setOverflowOrderNumber(orderNumber);
            EntityWrapper<GoodsOverflow> entityWrapper = new EntityWrapper<>(goodsOverflow);
            entityWrapper.like("overflow_order_number", orderNumber);
            Page<GoodsOverflow> page = new PageFactory<GoodsOverflow>().defaultPage();
            Page<GoodsOverflow> pageResult = overflowService.selectPageWithDataScope(page, entityWrapper);
            return this.packForBT(pageResult);
        }
        return new PageInfoBT<>(new Page<>());
    }

    /**
     * 新增商品录入管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(ManagerGoods managerGoods) {
        String createBy = Objects.requireNonNull(ShiroKit.getUser()).getAccount();
        managerGoods.setCreateBy(createBy);
        managerGoods.setUpdateBy(createBy);
        managerGoods.setGoodsInventoryQuantity(0);
        managerGoodsService.insert(managerGoods);
        return SUCCESS_TIP;
    }

    /**
     * 删除商品录入管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer managerGoodsId) {
        managerGoodsService.deleteById(Long.valueOf(managerGoodsId));
        return SUCCESS_TIP;
    }

    /**
     * 修改商品录入管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(ManagerGoods managerGoods) {
        String createBy = Objects.requireNonNull(ShiroKit.getUser()).getAccount();
        managerGoods.setUpdateBy(createBy);
        managerGoods.setUpdateDate(new Date());
        managerGoodsService.updateById(managerGoods);
        return SUCCESS_TIP;
    }

    /**
     * 商品录入管理详情
     */
    @RequestMapping(value = "/detail/{managerGoodsId}")
    @ResponseBody
    public Object detail(@PathVariable("managerGoodsId") Integer managerGoodsId) {
        return managerGoodsService.selectById(managerGoodsId);
    }
}
